<?php

class Filter_TableReferencesTable {
/* Doesn't work without the datasource */
	var $aTablesReferencingTable  = array();
	var $iDegreeOfSeparation;
	var $oDatasource;
	
	function Filter_TableReferencesTable( $oDatasource, $sTableName, $iDegreeOfSeparation = 1 ){
		$this->oDatasource = $oDatasource;
		$this->iDegreeOfSeparation = $iDegreeOfSeparation; 
		$this->_SetupReferenceArray( strtoupper( $sTableName ), 0 );
	}

	function _SetupReferenceArray( $sTableName, $iCurrentDegreeOfSeparation ) {
		
		if ( !in_array( $sTableName, $this->aTablesReferencingTable ) ) {
		
			$aForeignKeyList = $this->oDatasource->GetForeignKeys( $sTableName );
		
			$this->aTablesReferencingTable[] = $sTableName;
			$iCurrentDegreeOfSeparation++;
			
			if ( $iCurrentDegreeOfSeparation <= $this->iDegreeOfSeparation ) {
			
				foreach( $aForeignKeyList as $aThisForeignKey ) {
		
					if ( $aThisForeignKey['FROM_TABLE'] == $sTableName ) {
						$this->_SetupReferenceArray( $aThisForeignKey['TO_TABLE'], $iCurrentDegreeOfSeparation );
					}
					if ( $aThisForeignKey['TO_TABLE'] == $sTableName ) {
						$this->_SetupReferenceArray( $aThisForeignKey['FROM_TABLE'], $iCurrentDegreeOfSeparation );
					}
				}
			}
		}
	}

	function ShouldIncludeTable( $sTableName ) {
		return in_array( $sTableName, $this->aTablesReferencingTable );
	}
}


?>
